#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe


from aggmon.metric_container import MetricContainer


__all__ = [ "NagiosStatusLogMetricContainer" ]


class NagiosStatusLogMetricContainer(MetricContainer):
    """
    Determines when a metric changes and is worth publishing. If it doesn't change, it will be published
    if the timeout has passed. For nagios metrics the timeout is the same for all metrics.
    """
    def __init__( self, timeout=300 ):
        MetricContainer.__init__( self )
        self.timeout = timeout

    def eq( self, metric1, metric2 ):
        assert hasattr( metric2, "time" ), "time missing in Nagios metric:\n" + repr( metric2 )
        assert hasattr( metric2, "output" ), "output missing in Nagios metric:\n"  + repr( metric2 )
        assert hasattr( metric2, "value" ), "value missing in Nagios metric:\n" + repr( metric2 )

        #print repr( metric1 ), "\n", repr( metric2 )

        if metric2.time <= (metric1.time + self.timeout) and metric2.value == metric1.value and metric2.output == metric1.output:
            return True
        else:
            return False     